<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="关键字:" prop="name">
        <el-input
          size="mini"
          v-model="queryParams.name"
          placeholder="请输入关键字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分类名称:" prop="hcId">
        <el-select v-model="queryParams.hcId" placeholder="请选择状态" clearable size="mini">
          <el-option
            v-for="item in optionSelect"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态:" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="mini">
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="配置类型:" prop="isApp">
        <el-select v-model="queryParams.isApp" placeholder="请选择配置类型" clearable size="mini">
          <el-option
            v-for="dict in dict.type.is_app"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item v-if="queryParams.isApp == '0'" label="应用类型:" prop="appType">
        <el-select v-model="queryParams.appType" placeholder="请选择应用类型" clearable size="mini">
          <el-option
            v-for="dict in dict.type.app_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          size="mini" v-hasPermi="['system:handleApp:add']"
          @click="handleAdd"
        >添加
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="dataList">
      <el-table-column label="配置类型" prop="isApp" key="isApp" align="center" width="200">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.is_app" :value="scope.row.isApp"/>
        </template>
      </el-table-column>
      <el-table-column label="分类名称" prop="categorizeName" key="categorizeName" align="center" width="200"/>
      <el-table-column label="办事标题" :show-overflow-tooltip="true" prop="name" key="name" align="center"
                       width="200"/>
      <el-table-column label="应用名称(应用配置)" :show-overflow-tooltip="true" prop="appName" key="appName" align="center"
                       width="200"/>
      <el-table-column label="图标(应用配置)" key="appIcon" align="center" width="200">
        <template slot-scope="scope">
          <image-preview v-if="scope.row.appIcon" class="list-img" :src="scope.row.appIcon"/>
          <span v-else>暂无</span>
        </template>
      </el-table-column>
      <el-table-column label="描述(应用配置)" :show-overflow-tooltip="true" prop="appDescribe" key="appDescribe"
                       align="center" width="200"/>
      <el-table-column label="类型(应用配置)" prop="appType" key="appType" align="center" width="200">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.app_type" :value="scope.row.appType"/>
        </template>
      </el-table-column>
      <el-table-column label="跳转路由(应用配置)" :show-overflow-tooltip="true" prop="appRouteUrl" key="appRouteUrl"
                       align="center" width="200"/>
      <el-table-column label="机构名称(办事指南)" :show-overflow-tooltip="true" prop="agencyName" key="agencyName"
                       align="center" width="200"/>
      <el-table-column label="办公地址(办事指南)" :show-overflow-tooltip="true" prop="handleAddress" key="handleAddress"
                       align="center" width="200"/>
      <el-table-column label="办公时间(办事指南)" :show-overflow-tooltip="true" prop="handleTime" key="handleTime"
                       align="center" width="200"/>
      <el-table-column label="联系电话(办事指南)" :show-overflow-tooltip="true" prop="handlePhone" key="handlePhone"
                       align="center" width="200"/>
      <el-table-column label="状态" prop="status" key="status" align="center" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="排序号" prop="sort" key="sort" align="center" width="100"/>
      <el-table-column label="创建时间" prop="createTime" align="center" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" class-name="small-padding fixed-width" align="center" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleEdit(scope.row)"
            v-hasPermi="['system:handleApp:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:handleApp:remove']"
          >删除
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="goDetail(scope.row)"
            v-hasPermi="['system:handleApp:query']"
          >详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="分类名称:" prop="hcId">
          <el-select v-model="form.hcId" placeholder="请选择分类名称" clearable style="width: 100%">
            <el-option
              v-for="item in optionSelect"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="配置类型:" prop="isApp">
          <el-radio-group v-model="form.isApp">
            <el-radio
              v-for="dict in dict.type.is_app"
              :key="dict.value"
              :label="dict.value"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="办事标题:" prop="name">
          <el-input v-model="form.name" placeholder="请输入办事标题"/>
        </el-form-item>
        <el-form-item label="应用名称:" prop="appName">
          <el-input v-model="form.appName" placeholder="请输入应用名称"/>
        </el-form-item>
        <el-form-item v-if="form.isApp === '0'" label="应用图标:" prop="appIcon">
          <ImageUpload v-model="form.appIcon" :file-size="0.5" :limit="1"/>
        </el-form-item>
        <el-form-item v-if="form.isApp === '0'" label="应用描述:" prop="appDescribe">
          <el-input type="textarea" :rows="3" v-model="form.appDescribe" placeholder="请输入应用描述"/>
        </el-form-item>
        <el-form-item v-if="form.isApp === '0'" label="应用类型:" prop="appType">
          <el-select v-model="form.appType" placeholder="请选择应用类型" clearable style="width: 100%;">
            <el-option
              v-for="dict in dict.type.app_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.isApp === '0'" label="跳转路由:" prop="appRouteUrl">
          <el-input v-model="form.appRouteUrl" placeholder="请输入跳转路由"/>
        </el-form-item>
        <el-form-item v-if="form.isApp === '1'" label="机构名称:" prop="agencyName">
          <el-input v-model="form.agencyName" placeholder="请输入跳转路由"/>
        </el-form-item>
        <el-form-item v-if="form.isApp === '1'" label="办公地址:" prop="handleAddress">
          <el-input v-model="form.handleAddress" placeholder="请输入办公地址"/>
        </el-form-item>
        <el-form-item v-if="form.isApp === '1'" label="办公时间:" prop="handleTime">
          <el-input v-model="form.handleTime" placeholder="请输入办公时间"/>
        </el-form-item>
        <el-form-item v-if="form.isApp === '1'" label="联系电话:" prop="handlePhone">
          <el-input v-model="form.handlePhone" placeholder="请输入联系电话"/>
        </el-form-item>
        <el-form-item label="显示排序:" prop="sort">
          <el-input-number v-model="form.sort" controls-position="right" :min="0"/>
        </el-form-item>
        <el-form-item label="状态:" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio label="0">正常</el-radio>
            <el-radio label="1">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="详情" :visible.sync="detailOpen" width="800px" append-to-body>
      <el-form :model="detail" ref="form" label-width="100px">
        <el-row :gutter="24">
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="分类名称: ">
                {{ detail.categorizeName }}
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="配置类型: ">
                <dict-tag :options="dict.type.is_app" :value="detail.isApp"/>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="办事标题: ">
                {{ detail.name }}
              </el-form-item>
            </div>
          </el-col>
          <el-col v-if="detail.isApp === '0'" :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="应用图标: ">
                <span v-if="detail.appIcon === null || detail.appIcon === ''">暂无</span>
                <div class="viewer-list-img" v-else>
                  <viewer :images="detail.appIcon">
                    <image-preview v-for="src in detail.appIcon" :src="src" :key="src" class="detail-img"/>
                  </viewer>
                </div>
              </el-form-item>
            </div>
          </el-col>
          <el-col v-if="detail.isApp === '0'" :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="应用描述: ">
                {{ detail.appDescribe }}
              </el-form-item>
            </div>
          </el-col>
          <el-col v-if="detail.isApp === '0'" :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="应用类型: ">
                <dict-tag :options="dict.type.app_type" :value="detail.appType"/>
              </el-form-item>
            </div>
          </el-col>
          <el-col v-if="detail.isApp === '0'" :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="跳转路由: ">
                {{ detail.appRouteUrl }}
              </el-form-item>
            </div>
          </el-col>

          <el-col v-if="detail.isApp === '1'" :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="机构名称: ">
                {{ detail.agencyName }}
              </el-form-item>
            </div>
          </el-col>
          <el-col v-if="detail.isApp === '1'" :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="办公地址: ">
                {{ detail.handleAddress }}
              </el-form-item>
            </div>
          </el-col>
          <el-col v-if="detail.isApp === '1'" :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="办公时间: ">
                {{ detail.handleTime }}
              </el-form-item>
            </div>
          </el-col>
          <el-col v-if="detail.isApp === '1'" :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="联系电话: ">
                {{ detail.handlePhone }}
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="创建时间: ">
                {{ detail.createTime }}
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="状态: ">
                <dict-tag :options="dict.type.sys_normal_disable" :value="detail.status"/>
              </el-form-item>
            </div>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import {optionSelect, pageList, add, edit, getInfo, remove} from "@/api/triones/handleApp";

export default {
  name: "index",
  dicts: ['sys_normal_disable', 'is_app', 'app_type'],
  data() {
    return {
      showSearch: true,
      total: 0,
      dataList: [],
      loading: false,
      optionSelect: [],
      optionChildSelect: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        hcId: null,
        name: null,
        status: null,
        isApp: null
      },
      title: '',
      open: false,
      form: {},
      rules: {
        hcId: [
          {required: true, message: "分类不能为空", trigger: "blur"}
        ],
        isApp: [
          {required: true, message: "配置类型不能为空", trigger: "blur"}
        ],
        name: [
          {required: true, message: "办事标题不能为空", trigger: "blur"}
        ]
      },
      detailOpen: false,
      detail: {}
    }
  },
  created() {
    this.getList();
    this.getOptionSelect();
  },
  methods: {
    getOptionSelect() {
      optionSelect().then(res => {
        this.optionSelect = res.data;
      });
    },
    getList() {
      this.loading = true;
      pageList(this.queryParams).then(res => {
        this.dataList = res.rows;
        this.total = res.total;
        this.loading = false;
      })
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleAdd() {
      this.getOptionSelect();
      this.reset();
      this.title = "添加应用配置";
      this.open = true;
    },
    handleEdit(row) {
      this.reset();
      const id = row.id;
      getInfo(id).then(response => {
        this.form = response.data;
        this.title = "修改应用配置";
        this.open = true;
      })
    },
    reset() {
      this.form = {
        id: undefined,
        hcId: undefined,
        isApp: '0',
        name: undefined,
        appIcon: undefined,
        appDescribe: undefined,
        appType: undefined,
        appRouteUrl: undefined,
        agencyName: undefined,
        handleAddress: undefined,
        handleTime: undefined,
        handlePhone: undefined,
        status: '0',
        sort: 0
      }
      this.resetForm("form");
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.open = true;
          if (this.form.id != null) {
            edit(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            add(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleDelete(row) {
      this.$modal.confirm('是否确认删除办事标题为"' + row.name + '"的数据项？').then(function () {
        return remove(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    goDetail(row) {
      const id = row.id;
      getInfo(id).then(response => {
        this.detail = response.data;
        this.detailOpen = true;
      })
    }
  }
}

</script>
<style scoped lang="scss">

</style>
